class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def removeNthFromEnd( head: ListNode, n: int) -> ListNode:
    dummy_head = ListNode(0, head)

    slow = fast = dummy_head


    for i in range(n + 1):
        fast = fast.next

    while fast:
        slow = slow.next
        fast = fast.next

    # 通过更新第 (n-1) 个节点的 next 指针删除第 n 个节点
    slow.next = slow.next.next

    return dummy_head.next

